﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：批量调动局公司
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Web.UI;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.JiChuXX
{
    public partial class PiLiangDDJGS : BasePage
    {
        #region 属性

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();


        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 公司信息BLL
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 局信息BLL
        /// </summary>
        YS_JX_JUXINXBLL _YS_JX_JUXINXBLL = new YS_JX_JUXINXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///页面加载事件 
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {

            }
            LoadGongSiXXByJuHao(this.conYuanJuH, lblYuanJuMC, this.gvYuanJuGSList);
            LoadGongSiXXByJuHao(this.conMuBiaoJuH, lblMuBiaoJuMC, this.gvTargetJuGSList);
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            DataTable dt = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic("1=0", "", null);
            base.Render(sender);
        }

        /// <summary>
        /// 查询原局列表
        /// </summary>
        protected void btnYuanJuSearch_Click(object sender, EventArgs e)
        {
            if (this.conYuanJuH.Text.Trim() == "")
            {
                return;
            }
            if (this.conYuanJuH.Text.Trim() == this.conMuBiaoJuH.Text.Trim())
            {
                PageTools.AlertMsg(this,"目标局号与原局号不能相同！");
                return;
            }
            LoadGongSiXXByJuHao(this.conYuanJuH, lblYuanJuMC, this.gvYuanJuGSList);
        }

        /// <summary>
        /// 查询目的局列表
        /// </summary>
        protected void btnMuDiJuSearch_Click(object sender, EventArgs e)
        {
            if (this.conMuBiaoJuH.Text.Trim() == "")
            {
                return;
            }
            if (this.conYuanJuH.Text.Trim() == this.conMuBiaoJuH.Text.Trim())
            {
                 PageTools.AlertMsg(this,"目标局号与原局号不能相同！");
                return;
            }
            LoadGongSiXXByJuHao(this.conMuBiaoJuH, lblMuBiaoJuMC, this.gvTargetJuGSList);
        }

        /// <summary>
        /// 部分公司调动操作
        /// </summary>
        protected void btnBuFenGSDD_Click(object sender, EventArgs e)
        {
            //判断目的局是否存在
            if (this.conMuBiaoJuH.Text.Trim() == "") return;
            DataTable dt = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and I_LEIXING=1", this.conMuBiaoJuH.Text), null);
            if (dt.Rows.Count != 1) return;
            //获取需要调动的局下选中的公司ID
            string bhs = "";
            List<object> lSelectValues = this.gvYuanJuGSList.GetSelectedFieldValues("S_BIANHAO");
            for (int i = 0; i < lSelectValues.Count; i++)
            {
                bhs = bhs + lSelectValues[i] + ",";
            }
            if (bhs == "") return;
            //将选中项的主键组合成: ,ID,ID,ID, 的形式. 开头结尾都用"," 方便在数据库中使用charindex()条件匹配
            bhs = "," + bhs;
            //将选中公司更新到目标局
            _spHelper.P_UPDATEGONGSIXXTOTARGETJU(bhs, this.conMuBiaoJuH.Text.Trim());
            //重新加载公司信息
            this.LoadGongSiXXByJuHao(this.conYuanJuH, this.lblYuanJuMC, this.gvYuanJuGSList);
            this.LoadGongSiXXByJuHao(this.conMuBiaoJuH, this.lblMuBiaoJuMC, this.gvTargetJuGSList);      
             PageTools.AlertMsg(this,"操作成功！");
        }

        /// <summary>
        /// 全局调动操作
        /// </summary>
        protected void btnQuanJuDD_Click(object sender, EventArgs e)
        {
            //判断需调动局是否存在
            if (this.conYuanJuH.Text.Trim() == "") return;
            if (_YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and I_LEIXING=1", this.conYuanJuH.Value), null).Rows.Count != 1) return;
            //判断目的局是否存在
            if (this.conMuBiaoJuH.Text.Trim() == "") return;
            if (this.conYuanJuH.Text.Trim() == this.conMuBiaoJuH.Text.Trim())
            {
                PageTools.AlertMsg(this, "请输入不同的局！");
                return;
            }
            if (_YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and I_LEIXING=1", this.conMuBiaoJuH.Text), null).Rows.Count != 1) return;
            //执行全部调动
            _spHelper.P_UPDATEGONGSIJUHTOTARGET(this.conYuanJuH.Text.Trim(), this.conMuBiaoJuH.Text.Trim());
            PageTools.AlertMsg(this, "操作成功！");
            this.Status = GridViewStatus.Init;
            //重新加载公司信息
            this.LoadGongSiXXByJuHao(this.conYuanJuH, this.lblYuanJuMC, this.gvYuanJuGSList);
            this.LoadGongSiXXByJuHao(this.conMuBiaoJuH, this.lblMuBiaoJuMC, this.gvTargetJuGSList);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 加载局下公司
        /// </summary>
        /// <param name="juhao">局号</param>
        /// <param name="lblJuMingC">用于显示局名称的控件</param>
        /// <param name="gv">加载公司的gridview</param>
        /// <param name="alert">是否弹消息</param>
        private void LoadGongSiXXByJuHao(DevExpress.Web.ASPxTextBox txtJuH, DevExpress.Web.ASPxLabel lblJuMingC, DevExpress.Web.ASPxGridView gv)
        {            
            if(this.conYuanJuH.Text.Trim()==this.conMuBiaoJuH.Text.Trim())
            {
                if (txtJuH.ID == "conYuanJuH")
                {
                    gv.DataSource = this.dtSource;
                }
                else
                {
                    gv.DataSource = this.dtSource2;
                }
                gv.DataBind();
                return;
            }
            //局号
            string juHao = txtJuH.Text.Trim();
            if (juHao == "")
            {
                return;
            }
            //查询局名称
            DataTable dt = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and I_LEIXING=1", juHao), null);
            
            if (dt.Rows.Count != 1)
            {
                lblJuMingC.Text = "不存在此编号的局。";
                if (txtJuH.ID == "conYuanJuH")
                {
                    this.dtSource = null;
                }
                else
                {
                    this.dtSource2 = null;
                }
                gv.DataSource = null;
                gv.DataBind();
            }
            else
            {
                lblJuMingC.Text = dt.Rows[0]["S_MINGCHENG"].ToString();
                if (txtJuH.ID == "conYuanJuH")
                {
                    this.dtSource = this._YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_jubianh='{0}' and I_LEIXING=1", juHao), null);
                    gv.DataSource = this.dtSource;
                }
                else
                {
                    this.dtSource2 = this._YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_jubianh='{0}' and I_LEIXING=1", juHao), null);
                    gv.DataSource = this.dtSource2;
                }                
                gv.DataBind();
            }
        }

        #endregion
    }
}